﻿// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

using System;
using System.IO;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.ApplicationModel.Resources;
using log4net;
using WeAppsCenter.Core;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
namespace WeAppsCenter;


/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.InitializeComponent();
        if (!Directory.Exists(Settings.DefaultDirectory))
        {
            Directory.CreateDirectory(Settings.DefaultDirectory);
        }
        UnhandledException += App_UnhandledException;
    }
    public static ILog operationLog = LogManager.GetLogger("OperationLog");
    public static ILog errorLog = LogManager.GetLogger("ErrorLog");

    private async void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
    {
        ResourceLoader resourceLoader = new ResourceLoader();
        
        e.Handled = true;
        await new ContentDialog
        {
            XamlRoot = m_window.Content.XamlRoot,
            Title = resourceLoader.GetString("AnErrorOccurred"),
            Content = e.Message,
            CloseButtonText = resourceLoader.GetString("OK"),
            DefaultButton = ContentDialogButton.Close
        }.ShowAsync();
        errorLog.Error("错误", e.Exception);
    }

    /// <summary>
    /// Invoked when the application is launched.
    /// </summary>
    /// <param name="args">Details about the launch request and process.</param>
    protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
    {

        m_window = new MainWindow();
        m_window.Activate();
        // no UIElement is set for titlebar, fallback titlebar is created

    }
    public static Window m_window;
}
